package fr.fermeManager.application.views.components;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;
import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.SwingBindings;

import fr.fermeManager.library.model.beans.Transaction;
import fr.fermeManager.library.model.beans.TypeTransaction;



public class ComponentTransaction extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private BindingGroup m_bindingGroup;
	private Transaction transaction = new Transaction();
	private JCheckBox actifJCheckBox;
	private JTextField descritptionJTextField;
	private JTextField idJTextField;
	private JTextField libelleJTextField;
	private JTextField textFieldMontant;
	private JComboBox comboBox;
	
	private List<TypeTransaction> listeTypes = new ArrayList<TypeTransaction>();
	
	public ComponentTransaction() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0E-4 };
		gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0E-4 };
		setLayout(gridBagLayout);

		JLabel actifLabel = new JLabel("Actif:");
		GridBagConstraints labelGbc_0 = new GridBagConstraints();
		labelGbc_0.insets = new Insets(5, 5, 5, 5);
		labelGbc_0.gridx = 0;
		labelGbc_0.gridy = 0;
		add(actifLabel, labelGbc_0);

		actifJCheckBox = new JCheckBox();
		GridBagConstraints componentGbc_0 = new GridBagConstraints();
		componentGbc_0.insets = new Insets(5, 0, 5, 0);
		componentGbc_0.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_0.gridx = 1;
		componentGbc_0.gridy = 0;
		add(actifJCheckBox, componentGbc_0);

		JLabel descritptionLabel = new JLabel("Descritption:");
		GridBagConstraints labelGbc_1 = new GridBagConstraints();
		labelGbc_1.insets = new Insets(5, 5, 5, 5);
		labelGbc_1.gridx = 0;
		labelGbc_1.gridy = 1;
		add(descritptionLabel, labelGbc_1);

		descritptionJTextField = new JTextField();
		GridBagConstraints componentGbc_1 = new GridBagConstraints();
		componentGbc_1.insets = new Insets(5, 0, 5, 0);
		componentGbc_1.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_1.gridx = 1;
		componentGbc_1.gridy = 1;
		add(descritptionJTextField, componentGbc_1);

		JLabel idLabel = new JLabel("Id:");
		GridBagConstraints labelGbc_2 = new GridBagConstraints();
		labelGbc_2.insets = new Insets(5, 5, 5, 5);
		labelGbc_2.gridx = 0;
		labelGbc_2.gridy = 2;
		add(idLabel, labelGbc_2);

		idJTextField = new JTextField();
		GridBagConstraints componentGbc_2 = new GridBagConstraints();
		componentGbc_2.insets = new Insets(5, 0, 5, 0);
		componentGbc_2.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_2.gridx = 1;
		componentGbc_2.gridy = 2;
		add(idJTextField, componentGbc_2);

		JLabel libelleLabel = new JLabel("Libelle:");
		GridBagConstraints labelGbc_3 = new GridBagConstraints();
		labelGbc_3.insets = new Insets(5, 5, 5, 5);
		labelGbc_3.gridx = 0;
		labelGbc_3.gridy = 3;
		add(libelleLabel, labelGbc_3);

		libelleJTextField = new JTextField();
		GridBagConstraints componentGbc_3 = new GridBagConstraints();
		componentGbc_3.insets = new Insets(5, 0, 5, 0);
		componentGbc_3.fill = GridBagConstraints.HORIZONTAL;
		componentGbc_3.gridx = 1;
		componentGbc_3.gridy = 3;
		add(libelleJTextField, componentGbc_3);
		
		JLabel lblLibelle = new JLabel("Montant:");
		GridBagConstraints gbc_lblLibelle = new GridBagConstraints();
		gbc_lblLibelle.insets = new Insets(0, 0, 5, 5);
		gbc_lblLibelle.gridx = 0;
		gbc_lblLibelle.gridy = 4;
		add(lblLibelle, gbc_lblLibelle);
		
		textFieldMontant = new JTextField();
		GridBagConstraints gbc_textFieldMontant = new GridBagConstraints();
		gbc_textFieldMontant.insets = new Insets(0, 0, 5, 0);
		gbc_textFieldMontant.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldMontant.gridx = 1;
		gbc_textFieldMontant.gridy = 4;
		add(textFieldMontant, gbc_textFieldMontant);
		textFieldMontant.setColumns(10);
		
		JLabel lblType = new JLabel("Type");
		GridBagConstraints gbc_lblType = new GridBagConstraints();
		gbc_lblType.insets = new Insets(0, 0, 0, 5);
		gbc_lblType.gridx = 0;
		gbc_lblType.gridy = 5;
		add(lblType, gbc_lblType);
		
		comboBox = new JComboBox();
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 5;
		comboBox.setRenderer(new DefaultListCellRenderer(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public Component getListCellRendererComponent(JList list,
					Object value, int index, boolean isSelected,
					boolean cellHasFocus) {
				super.getListCellRendererComponent(list, value, index, isSelected,cellHasFocus);
				if(value instanceof TypeTransaction){
					TypeTransaction typeTransacTmp = ((TypeTransaction)value);
					setText(StringUtils. repeat("--", typeTransacTmp.getLevel()*2)+" " +typeTransacTmp.getLibelle());
				}
				return this;
			}
		});
		add(comboBox, gbc_comboBox);

		if ((transaction != null) || (listeTypes!=null)){
			m_bindingGroup = initDataBindings();
		}
	}

	public Transaction getTransaction() {
		return transaction;
	}

	public void setDonnees(Transaction newTransaction,List<TypeTransaction> listeTypesParam) {
		this.listeTypes =  listeTypesParam;
		setTransaction(newTransaction, true);
	}

	public void setTransaction(Transaction newTransaction,boolean update) {
		transaction = newTransaction;
		if (update) {
			if (m_bindingGroup != null) {
				m_bindingGroup.unbind();
				m_bindingGroup = null;
			}
			if (transaction != null) {
				m_bindingGroup = initDataBindings();
			}
		}
		
		for (int i = 0; i < this.listeTypes.size(); i++) {
			if(this.listeTypes.get(i).getId() == newTransaction.getId()){
				comboBox.setSelectedItem(this.listeTypes.get(i));
				System.out.println("------"+this.listeTypes.get(i).getLibelle());
			}
		}
		
		System.out.println(newTransaction.getTypeTransaction());
		
		
	}
	protected BindingGroup initDataBindings() {
		BeanProperty<Transaction, Boolean> actifProperty = BeanProperty.create("actif");
		BeanProperty<JCheckBox, String> textProperty = BeanProperty.create("text");
		AutoBinding<Transaction, Boolean, JCheckBox, String> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, transaction, actifProperty, actifJCheckBox, textProperty);
		autoBinding.bind();
		//
		BeanProperty<Transaction, String> descritptionProperty = BeanProperty.create("descritption");
		BeanProperty<JTextField, String> textProperty_1 = BeanProperty.create("text");
		AutoBinding<Transaction, String, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, transaction, descritptionProperty, descritptionJTextField, textProperty_1);
		autoBinding_1.bind();
		//
		BeanProperty<Transaction, Integer> idProperty = BeanProperty.create("id");
		BeanProperty<JTextField, String> textProperty_2 = BeanProperty.create("text");
		AutoBinding<Transaction, Integer, JTextField, String> autoBinding_2 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, transaction, idProperty, idJTextField, textProperty_2);
		autoBinding_2.bind();
		//
		BeanProperty<Transaction, String> libelleProperty = BeanProperty.create("libelle");
		BeanProperty<JTextField, String> textProperty_3 = BeanProperty.create("text");
		AutoBinding<Transaction, String, JTextField, String> autoBinding_3 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, transaction, libelleProperty, libelleJTextField, textProperty_3);
		autoBinding_3.bind();
		//
		BeanProperty<Transaction, Float> transactionBeanProperty = BeanProperty.create("montant");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<Transaction, Float, JTextField, String> autoBinding_4 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, transaction, transactionBeanProperty, textFieldMontant, jTextFieldBeanProperty);
		autoBinding_4.bind();
		//
		JComboBoxBinding<TypeTransaction, List<TypeTransaction>, JComboBox> jComboBinding = SwingBindings.createJComboBoxBinding(UpdateStrategy.READ_ONCE, listeTypes, comboBox);
		jComboBinding.bind();
		//
		BeanProperty<JComboBox, Boolean> jComboBoxBeanProperty = BeanProperty.create("selectedItem.actif");
		BeanProperty<Transaction, Boolean> transactionBeanProperty_1 = BeanProperty.create("typeTransaction.actif");
		AutoBinding<JComboBox, Boolean, Transaction, Boolean> autoBinding_5 = Bindings.createAutoBinding(UpdateStrategy.READ, comboBox, jComboBoxBeanProperty, transaction, transactionBeanProperty_1);
		autoBinding_5.bind();
		//
		BeanProperty<JComboBox, String> jComboBoxBeanProperty_1 = BeanProperty.create("selectedItem.descritption");
		BeanProperty<Transaction, String> transactionBeanProperty_2 = BeanProperty.create("typeTransaction.descritption");
		AutoBinding<JComboBox, String, Transaction, String> autoBinding_6 = Bindings.createAutoBinding(UpdateStrategy.READ, comboBox, jComboBoxBeanProperty_1, transaction, transactionBeanProperty_2);
		autoBinding_6.bind();
		//
		BeanProperty<JComboBox, Boolean> jComboBoxBeanProperty_2 = BeanProperty.create("selectedItem.estSortie");
		BeanProperty<Transaction, Boolean> transactionBeanProperty_3 = BeanProperty.create("typeTransaction.estSortie");
		AutoBinding<JComboBox, Boolean, Transaction, Boolean> autoBinding_7 = Bindings.createAutoBinding(UpdateStrategy.READ, comboBox, jComboBoxBeanProperty_2, transaction, transactionBeanProperty_3);
		autoBinding_7.bind();
		//
		BeanProperty<JComboBox, Integer> jComboBoxBeanProperty_3 = BeanProperty.create("selectedItem.id");
		BeanProperty<Transaction, Integer> transactionBeanProperty_4 = BeanProperty.create("typeTransaction.id");
		AutoBinding<JComboBox, Integer, Transaction, Integer> autoBinding_8 = Bindings.createAutoBinding(UpdateStrategy.READ, comboBox, jComboBoxBeanProperty_3, transaction, transactionBeanProperty_4);
		autoBinding_8.bind();
		//
		BeanProperty<JComboBox, Integer> jComboBoxBeanProperty_4 = BeanProperty.create("selectedItem.idParent");
		BeanProperty<Transaction, Integer> transactionBeanProperty_5 = BeanProperty.create("typeTransaction.idParent");
		AutoBinding<JComboBox, Integer, Transaction, Integer> autoBinding_9 = Bindings.createAutoBinding(UpdateStrategy.READ, comboBox, jComboBoxBeanProperty_4, transaction, transactionBeanProperty_5);
		autoBinding_9.bind();
		//
		BeanProperty<JComboBox, Integer> jComboBoxBeanProperty_5 = BeanProperty.create("selectedItem.level");
		BeanProperty<Transaction, Integer> transactionBeanProperty_6 = BeanProperty.create("typeTransaction.level");
		AutoBinding<JComboBox, Integer, Transaction, Integer> autoBinding_10 = Bindings.createAutoBinding(UpdateStrategy.READ, comboBox, jComboBoxBeanProperty_5, transaction, transactionBeanProperty_6);
		autoBinding_10.bind();
		//
		BeanProperty<JComboBox, String> jComboBoxBeanProperty_6 = BeanProperty.create("selectedItem.libelle");
		BeanProperty<Transaction, String> transactionBeanProperty_7 = BeanProperty.create("typeTransaction.libelle");
		AutoBinding<JComboBox, String, Transaction, String> autoBinding_11 = Bindings.createAutoBinding(UpdateStrategy.READ, comboBox, jComboBoxBeanProperty_6, transaction, transactionBeanProperty_7);
		autoBinding_11.bind();
		//
		AutoBinding<Transaction, Boolean, JComboBox, Boolean> autoBinding_12 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, transaction, transactionBeanProperty_1, comboBox, jComboBoxBeanProperty);
		autoBinding_12.bind();
		//
		AutoBinding<Transaction, String, JComboBox, String> autoBinding_13 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, transaction, transactionBeanProperty_2, comboBox, jComboBoxBeanProperty_1);
		autoBinding_13.bind();
		//
		AutoBinding<Transaction, Boolean, JComboBox, Boolean> autoBinding_14 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, transaction, transactionBeanProperty_3, comboBox, jComboBoxBeanProperty_2);
		autoBinding_14.bind();
		//
		AutoBinding<Transaction, Integer, JComboBox, Integer> autoBinding_15 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, transaction, transactionBeanProperty_4, comboBox, jComboBoxBeanProperty_3);
		autoBinding_15.bind();
		//
		AutoBinding<Transaction, Integer, JComboBox, Integer> autoBinding_16 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, transaction, transactionBeanProperty_5, comboBox, jComboBoxBeanProperty_4);
		autoBinding_16.bind();
		//
		AutoBinding<Transaction, Integer, JComboBox, Integer> autoBinding_17 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, transaction, transactionBeanProperty_6, comboBox, jComboBoxBeanProperty_5);
		autoBinding_17.bind();
		//
		AutoBinding<Transaction, String, JComboBox, String> autoBinding_18 = Bindings.createAutoBinding(UpdateStrategy.READ_ONCE, transaction, transactionBeanProperty_7, comboBox, jComboBoxBeanProperty_6);
		autoBinding_18.bind();
		//
		BindingGroup bindingGroup = new BindingGroup();
		//
		bindingGroup.addBinding(autoBinding);
		bindingGroup.addBinding(autoBinding_1);
		bindingGroup.addBinding(autoBinding_2);
		bindingGroup.addBinding(autoBinding_3);
		bindingGroup.addBinding(autoBinding_4);
		bindingGroup.addBinding(jComboBinding);
		bindingGroup.addBinding(autoBinding_5);
		bindingGroup.addBinding(autoBinding_6);
		bindingGroup.addBinding(autoBinding_7);
		bindingGroup.addBinding(autoBinding_8);
		bindingGroup.addBinding(autoBinding_9);
		bindingGroup.addBinding(autoBinding_10);
		bindingGroup.addBinding(autoBinding_11);
		bindingGroup.addBinding(autoBinding_12);
		bindingGroup.addBinding(autoBinding_13);
		bindingGroup.addBinding(autoBinding_14);
		bindingGroup.addBinding(autoBinding_15);
		bindingGroup.addBinding(autoBinding_16);
		bindingGroup.addBinding(autoBinding_17);
		bindingGroup.addBinding(autoBinding_18);
		return bindingGroup;
	}
}
